//
//  ViewController.m
//  23-QQMusic
//
//  Created by ParkourH on 16/8/15.
//  Copyright © 2016年 ParkourH. All rights reserved.
//

#import "ViewController.h"
#import "AFNetworking.h"
#import "MVModel.h"
#import "MVData.h"
#import "MVContents.h"
#import "MVTableList.h"
#import "CellOfTableView.h"

#define kTableViewCellReuse @"reuse"


@interface ViewController () <UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *qq_MVTableView;

@property (nonatomic, strong) MVModel *mvModel;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self handleData];
}

#pragma mark ------------  解析数据  -----------
- (void)handleData {

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:@"http://i.y.qq.com/v8/fcg-bin/getmv_by_tag?cmd=mv_shows_new&format=jsonp&g_tk=5381&uin=0&format=json&inCharset=utf-8&outCharset=utf-8&notice=0&platform=h5&needNewCode=1&_=1470996835294&jsonpCallback=jsonp1" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSData *data = responseObject;
        
        NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSString *string = [dataString substringWithRange:NSMakeRange(7, dataString.length - 8)];
        NSData *jsonData = [string dataUsingEncoding:NSUTF8StringEncoding];
        
        NSError *error = nil;
        id result = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
        
        self.mvModel = [MVModel baseModelWithDic:result];
        
        [self.qq_MVTableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}


#pragma mark ------------  tableView 的协议方法 -----------
/** 分区数 */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return self.mvModel.data.contents.list.count;
}

/** 分区中的cell个数 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 1;
}
/** 返回cell的协议 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    CellOfTableView *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewCellReuse];
    cell.mvListArray = self.mvModel.data.contents.list[indexPath.section].mvlist;
    
    return cell;
}

/** 返回区标题 */
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    return @"sdb ja";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
